import random
import time
from thing import fetch,Thing
from textwrap import wrap
import color
import time
import calculator

"""
This module will contain everything that can be manipulated by the player in the game word
with the exception of doors. So, levers, buttons, drawbridges, whatever.  

Naming scheme:
    <type>_<effect>
    ex. lever_openclose
    ex. button_castpell

This organized naming scheme will make it easier to identify what the device does, how it can
be implemented, and for easier use in using in multiple instances.Each new object should be 
its own Class.
"""

def displayText(actor, textRaw, color=color.darkgreen):
    "Format textRaw, and display to actor"
    textForm = wrap(textRaw, 70)
    for line in textForm:
        actor.hear(line, color)
    actor.hear("")
def itemCheck(actor, itemToFind):
    "Search actor's inventory for an item by *name*. Returns True if item is found. False, otherwise."
    has = False
    for uid in actor.contents:
        item = fetch(uid)
        if item == None: continue
        if item.name == itemToFind:
            has = True  
            break
    if has == False: return False
    else: return True

# The following classes are still very much in develpment, and are subject to drastic changes.
# Be cautious before using at this time.

class Device(Thing):
    "Basic class for opening doors"
    def __init__(self):
        Thing.__init__(self)
        self.attrs.add('device')
        self.on = False
        self.location = None
        self.controls = {'room': None, 'direction': None}
        self.onMsg   = None  # Message displayed when device is switched on
        self.onMsg2  = None  # Optional message if there is a delay when switching device
        self.onDesc  = None  # Description while device is switched on
        self.offMsg  = None  # Message displayed when device is switched off
        self.offMsg2 = None  # Optional message if there is a delay when switching device
        self.offDesc = None  # Description while device is switched off

    def activate(self, actor):
        if self.on == False:
            if self.action == 'open close':
                fetch(self.controls['room']).doors[self.controls['direction']] = 0
                displayText(actor, self.onMsg)
                if self.onMsg2 != None:
                    time.sleep(2)
                    diplayText(actor, self.onMsg2)
                self.on = True
                self.desc = self.onDesc
            elif self.action == 'explode room':
                room = fetch(self.controls['room'])
                if room == None: return
                for uid in room.contents:
                    person = fetch(uid)
                    if person == None: continue
                    displayText(person,"You hear a click coming from behind the walls...")
                    time.sleep(3)
                x = room.contents
                for uid in x:
                    person = fetch(uid)
                    if person == None: continue
                    displayText(person,
                        "The walls around you flash orange and yellow as your body is engulfed in flame! "
                        "You feel nothing but pain as the fire sears your clothing to your flesh.", color.red)
                    if 'person' in person.attrs:
                        person.HP -= self.damage
                        calculator.prompt(person)
                        person.deathCheck(person) # Just realized how horrible this class method is...              
        else:
            displayText(actor, self.offMsg)
            if self.offMsg2 != None:
                time.sleep(2)
                displayText(actor, self.offMsg2)
            self.on = False
            self.desc = self.offDesc
            fetch(self.controls['room']).doors[self.controls['direction']] = 1

class Lever_openclose(Device):
    def __init__(self):
        Device.__init__(self)
        self.name = 'lever'
        self.action = 'open close'
        self.onMsg   = "The lever is difficult to pull, and grates against the inside of its casing. "\
                       "It gives at the last inch and snaps down loudly."
        self.onDesc  = "An older rusty lever attached to the wall, angled down."
        self.offMsg  = "It takes significant force to lift the lever, which doesn't seem to budge."
        self.offMsg2 = "You then give it one last push, and the lever then slams "\
                       "into the upward position with a loud snap. You hear something clinking behind the wall."
        self.offDesc = "An older rusty lever attached to the wall, angled up."
        self.desc = self.offDesc

class Button_explodeRoom(Device):
    def __init__(self):
        Device.__init__(self)
        self.name = 'button'
        self.action = 'explode room'
        self.damage = 30
        self.onMsg = "You press down on the button and after a few seconds feel more than hear a booming sound in the distance"
        self.onMsg2 = "The button slowly rises back out of its socket to its original position."


DEVICE_LIST = [
    (['lever_openclose'], Lever_openclose),
    (['button_exploderoom'], Button_explodeRoom),
]


